<template>
  <div class="addDirect">
    <el-dialog title="发送消息" :visible.sync="isShow" center :before-close="close">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-position="left" label-width="120px">
        <el-form-item label="订单类型" v-if='role_id == 4'>
          <el-radio-group v-model="orderType" @change="orderTypeChange">
            <el-radio :label="1">国知局前</el-radio>
            <el-radio :label="2">国知局后</el-radio>
            <el-radio :label="3">全部订单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="订单编号" prop="order">
          <el-select
            filterable
            placeholder="请选择订单编号"
            @change='orderChanged'
            v-model="form.order">
            <el-option
              v-for="item in orderOptions"
              :key="item.id"
              :label="item.order_number"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接收人" prop="receiver">
          <el-select v-model="form.receiver" placeholder="请选择接收人" @change="receiverChange">
            <template v-if="$store.state.user.is_teacher == 2">
              <el-option label="导师" value="2"></el-option>
            </template>
            <template v-else>
              <el-option label="发明人" value="5" v-if="/^(2|4)$/.test(role_id)"></el-option>
              <el-option label="审核员" value="3" v-if="/^(2)$/.test(role_id) && ($store.state.user.is_teacher != 2)"></el-option>
              <el-option label="代理人" value="2" v-if="/^(3|4|5)$/.test(role_id)"></el-option>
              <el-option label="专属顾问" value="4" v-if="/^(2|5)$/.test(role_id)"></el-option>
              <el-option label="徒弟" value="2" v-if="/^(2)$/.test(role_id) && $store.state.user.is_teacher == 1" :disabled='studentDisabled'></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="消息内容" prop="news">
          <el-input type="textarea" :rows="4" placeholder="请输入内容" v-model="form.news"></el-input>
        </el-form-item>


      </el-form>
      <div slot="footer" >
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      isShow: false,
      form: {
        order: null,
        receiver: '',
        news: ''
      },
      orderOptions: [],
      rules: {
        order: [
          { required: true, message: '请选择订单编号', trigger: 'change' }
        ],
        receiver: [
          { required: true, message: '请选择接收人', trigger: 'change' }
        ],
        news: [
          { required: true, message: '请输入消息内容', trigger: 'change' }
        ],
      },
      role_id: this.$store.state.user.role_id,
      receiver_id: null,
      order: null,
      studentDisabled: false,
      studentAlert: false,
      orderType: 1
    }
  },
  watch: {
    form: {
      handler: function(val) {
        if (val.receiver != 2) {
          this.studentAlert = false;
        }
      },
      deep: true,
    },
    order: function(val) {
      if (val.student_id) {
        this.studentDisabled = false
        this.studentAlert = false
      } else {
        if (this.$store.state.user.is_teacher != 2) {
          this.studentDisabled = true
          this.studentAlert = true
        }
      }
    },
    deep: true,
  },
  methods: {
    show() {
      this.isShow = true
      this.getOrderData(this.role_id)
    },
    close() {
      this.$refs['ruleForm'].resetFields()
      this.receiver_id = null
      this.isShow = false
    },
    submit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.getReceiver()
          if (this.studentAlert) {

            this.$alert('该订单无法对徒弟发起私信！', '提示', {
              confirmButtonText: '确定',
              callback: action => {
              }
            })
            return
          }
          this.submitPost()
        } else {
          return false;
        }
      })
    },
    submitPost () {
      let params = {
        news: this.form.news,
        receive_uid: this.receiver_id
      }
      if (this.order.finals_id) {
        params.finals_id = this.order.finals_id
      } else {
        params.order_id = this.order.id
      }

      this.$axios.post('api/news/add_private_letter', params).then(res => {
        if (res.data.code == 200) {
          this.$message({
            type: 'success',
            message:"发送私信成功！"
          })
          this.$emit('refresh')
          this.close()
        }
      })
    },
    // 获取角色订单
    getOrderData (role) {
      let url = ''
      let params = {}
      if (role == 2) {
        url = `api/news/get_agent_order_info`
      } else if (role == 5) {
        url = `api/news/get_inventor_order_info`
      } else if (role == 4) {
        url = 'api/news/get_process_order_info'
        params = {
          type: 1
        }
      } else if (role == 3) {
        url = 'api/news/get_auditor_order_info'
      }
      this.getOrderRequest(url,params)

    },
    getOrderRequest (url, params) {
      this.$axios.get(url, {params: params}).then( res => {
        if (res.data.code == 200) {
          if (this.role_id == 4 || this.role_id == 3) {
            let data = res.data.data
            for (let i = 0; i < data.length; i++) {
              if (data[i].finals_id) {
                data[i].id = data[i].finals_id
                data[i].order_number = data[i].finals_order_number
              }
            }
            this.orderOptions = data
          } else {
            this.orderOptions = res.data.data
          }

        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    // 获取接受人id
    getReceiver(){
      if (this.form.receiver == 5) {
        this.receiver_id = this.order.consumer_id
      } else if (this.form.receiver == 3) {
        this.receiver_id = this.order.auditor_id
      } else if (this.form.receiver == 2) {
        if (this.role_id == 2) {
          // 如果有student_id 就是分配的订单
          if (this.order.student_id) {
            // 如果角色是徒弟就取agent_id 反之取student_id
            if (this.$store.state.user.is_teacher == 2) {
              this.receiver_id = this.order.agent_id
            } else {
              this.receiver_id = this.order.student_id
            }
          } else {
            // 如果不是分配的 是徒弟自己的订单就取 他的id
            if (this.$store.state.user.is_teacher == 2) {
              this.receiver_id = this.order.agent.parent_id
            } else {
              // 如果是导师自己的订单就无法对徒弟发起私信
              this.studentDisabled = true
              this.studentAlert = true
            }
          }
        } else if (this.role_id == 4 || this.role_id == 3) {
          // 判断此订单有无导师 审核员、流程只对导师发私信
          if (this.order.agent_id == this.order.teacher_id) {
            this.receiver_id = this.order.agent_id
            this.studentDisabled = false
            this.studentAlert = false
          } else {
            this.receiver_id = this.order.teacher_id
          }
        }
      } else if (this.form.receiver == 4) {
        this.receiver_id = this.order.process_id
      }
    },
    orderChanged (value) {
      let ordersIndex = this.orderOptions.findIndex( item => item.id == value)
      this.order = this.orderOptions[ordersIndex]
    },
    receiverChange(value) {

    },
    orderTypeChange(value) {
      let params = {
      }
      if (value == 1 || value == 2) {
        params.type = value
        this.getOrderRequest('api/news/get_process_order_info',params)
      } else {
        this.getOrderRequest('api/news/get_process_order_info',params)
      }


    }
  }
}
</script>

<style lang='less' type='text/css'>
  .addDirect {
    .el-select {
      width: 100%;
    }
    .el-dialog {
      max-width: 700px;
    }
  }
</style>
